package com.capinfo.accounting.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.google.gson.Gson;
import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.HZJZPZ;
import com.capinfo.accumulation.model.accounting.mybist.KMSZJBSJ;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.accumulation.util.NumberToCN;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;


@Controller
@RequestMapping("/business/accounting/zdpzgl")
public class ZDPZGLController  {
	

	@Autowired
	HZJZPZServiceI hZJZPZService;
	
	@Autowired
	GB_JZPZXXServiceI  gB_JZPZXXServiceI;
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		
		return hZJZPZService.findbyByGrid(param);
	}
	
	
	@RequestMapping(value="/hsdw")  
	@ResponseBody  
	public void hsdw (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		 Gson gson = new Gson();
		   Map map = new HashMap();
		   List<Map<String,Object>>hsdw= hZJZPZService.hsdw();
		   PrintWriter pw;
		   pw = response.getWriter();
		   pw.write(gson.toJson(hsdw));
	   }
	
	
	@RequestMapping(value="/hxhsdw")  
	@ResponseBody  
	public void hxhsdw (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		String ID = request.getParameter("ID");
		 Gson gson = new Gson();
		   Map map = new HashMap();
		   map.put("ID", ID);
		   List<Map<String,Object>>hsdw= hZJZPZService.huixianhsdw(map);
		   PrintWriter pw;
		   pw = response.getWriter();
		   pw.write(gson.toJson(hsdw));
	   }
	

	
	
	@RequestMapping(value="/cwzt")  
	@ResponseBody  
	public void cwzt (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		 Gson gson = new Gson();
		   Map map = new HashMap();
		   List<Map<String,Object>>cwzt= hZJZPZService.cwzt();
		   PrintWriter pw;
		   pw = response.getWriter();
		   pw.write(gson.toJson(cwzt));
	   }

	
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(HZJZPZ t , HttpSession session,NativeWebRequest request) {
		
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		try {
			hZJZPZService.insert(t);
			j.setSuccess(true);
			j.setMsg("添加成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	@RequestMapping("/update.shtml")
	@ResponseBody
	public Json update(HZJZPZ t , HttpSession session,NativeWebRequest request) throws ParseException {
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
		t.setJZRQ(time.parse(t.getJzrq()));
		t.setFHRQ(time.parse(t.getFhrq()));
		t.setPZRQ(time.parse(t.getPzrq()));
		t.setZDRQ(time.parse(t.getZdrq()));
		Long HSDW=112L;
		t.setHSDW(HSDW);
		Json j = new Json();
		try {
			hZJZPZService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
		
		}
	
	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(HZJZPZ t , HttpSession session,NativeWebRequest request) {
		
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		try {
			
			hZJZPZService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("保存成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			hZJZPZService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/selectByid.shtml")
	@ResponseBody
	public HZJZPZ selectByid(String id) {
		return hZJZPZService.selectByPrimaryKey(id);
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/manager.shtml")
	public String manager() {
		return "/business/accounting/zdpzgl/ZDPZGL";
	}
	
	/**
	 * 
	 * @return
	 */
	
	
	@RequestMapping("/addPage.shtml")
	public String addPage(HttpServletRequest request) {
		HZJZPZ hzjzpz  = new HZJZPZ();
		request.setAttribute("HZJZPZ", hzjzpz);
		return "/business/accounting/zdpzgl";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/editPage.shtml")
	public String editPage(String ID,HttpServletRequest request) {
		HZJZPZ hzjzpz = this.hZJZPZService.selectByPrimaryKey(ID);
		request.setAttribute("HZJZPZ", hzjzpz);
		String FH = hzjzpz.getFH();
		String JZ= hzjzpz.getJZ();
		if(JZ==null){
		if(FH==null){
		return "/business/accounting/zdpzgl/ZDPZGLUpdate";
		}else
		{
			return "/business/accounting/zdpzgl/ZDPZGLUpdate1";
		}
		}else{
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage.shtml")
	public String viewPage(HttpServletRequest request,Map map) {
		String JZRQ = request.getParameter("JZRQ");
		String XSPZH = request.getParameter("XSPZH");
		map.put("JZRQ_EQ_DATE_yyyyMMdd", JZRQ);
 		map.put("JZRQ_END_DATE_yyyyMMdd", JZRQ);
		map.put("XSPZH_EQ_STR", XSPZH);
		DataGrid dd= this.gB_JZPZXXServiceI.findbyByGrid(map);
		List list = dd.getRows();
		Map mmm=new HashMap();
		List list1 = new ArrayList();
		double j= 0;
		double d= 0;
		   for(int i=0; i<list.size(); i++)    {   
			   GB_JZPZXX gb=   (GB_JZPZXX) list.get(i); 
			   j+=gb.getJFFSE();
			   d+=gb.getDFFSE();
		   } 
		    String nj = NumberToCN.number2CNMontrayUnit(new BigDecimal(j));
	        String nd = NumberToCN.number2CNMontrayUnit(new BigDecimal(d));
		   mmm.put("DFFSE", d);
		   mmm.put("JFFSE", j);
		   mmm.put("dDFFSE", nd);
		   mmm.put("dJFFSE", nj);
		   list1.add(mmm);
		request.setAttribute("list1",list1);   
		request.setAttribute("ddList",list);
		Map mm=new HashMap();
		mm.put("XSPZH", XSPZH);
		mm.put("JZRQ", JZRQ);
		List<Map<String, Object>> HZ=hZJZPZService.xianshihsdw(mm);
		request.setAttribute("HZ", HZ);
		return "/business/accounting/zdpzgl/chakan";
	}
	
}
